package com.example.depressionapp;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class PicturePlay {

    ImageView imageView;
    private int i = 0;
    Timer timer = new Timer();
    int[] images = { R.drawable.negative1, R.drawable.negative2, R.drawable.negative3, };
    int num = 0;

    public PicturePlay(ImageView imageView){
        this.imageView = imageView;
    }

    public void play(){
        timer.schedule(task,0,3000);
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                imageView.setImageResource(images[num++]);
            }else if(msg.what == 0){
                imageView.setImageDrawable(null);
            }
            if (num >= images.length) {
                timer.cancel();
            }
        }
    };

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            Message msg = new Message();
            if (i == 0) {
                msg.what = 0;
                i += 1;
            } else {
                msg.what = 1;
                i -= 1;
            }
            handler.sendMessage(msg);
        }
    };

}
